import getCrowerData from "../backend/crower.js"
import { BigPicture } from "../api/index.js"
import { getRandomImages } from "../utils/index.js"
import { makePicLocal } from "../utils/filesBox.js"
import delay from "../utils/delay.js"
import { downloadUrl, removeFile } from "../utils/fs.js"
const getBq = async (content, room) => {
  try {

    let key = content.split("#")[1] || content.split('＃')[1]
    // console.log(key.split('*'))

    let rule = `用'#' 拼接你想要的表情包类型，用'*' 拼接你想要的数量（不超过10张）,如：搜表情#蔡徐坤*2`
    if (!key) {
      room.say(rule)
      console.log('搜到啊')
      return
    }
    let num = key.split('*')[1] || 1
    console.log(num)
    const res = await BigPicture({ key,pageSize:999 })
    console.log('搜到包')
    console.log( res.data.items)
    const arr = res.data.items
    num = arr.length>num ? num : arr.length
    if(num>10) num=10
    // console.log(arr)
    const result = getRandomImages(arr, num || 1)
    if (result.length) {
      let hasSaylist = []
      await Promise.all(result.map(async it => {
        const data = await downloadUrl(it.url)
        const fileBox = makePicLocal(data.name)
        hasSaylist.push({url:data.name,fileBox})

      }))
      hasSaylist.map(async it => {
        console.log(it)
        if(it.fileBox){
          delay(1000)
          await room.say(it.fileBox)
          delay(2000)
          removeFile(it.url)
        }
      })
    } else {
      room.say('找不到这类表情，去网上搜搜吧')

    }

  } catch (e) {
    console.log(e)
  }
}

const getBQ = (key) => {
  https://doutu.lccyy.com/doutu/items?keyword=%E8%80%81%E5%AD%90%E7%BB%99%E4%BD%A0%E4%B8%80%E4%B8%AA%E5%A4%A7%E9%80%BC%E6%96%97&pageNum=1&pageSize=999
  url = `https://doutu.lccyy.com/doutu/items?keyword=${key}`
  var options = {
    'method': 'get',
    'url': encodeURI(url)
  }
  return new Promise((resolve, roject) => {
    request(options, function (error, response, body) {
      // 如果请求成功且状态码为 200
      if (error) {
        console.log(error)
        resolve(error)
        return
      }

      resolve(response.body)
    });
  })

}

export default getBq